package org.zgy.stock.threads.holder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.zgy.stock.model.holder.StockHolderNumsInfo;
import org.zgy.stock.service.holder.StockHolderNumsInfoService;
import org.zgy.stock.threads.DfcfResponse;
import org.zgy.stock.utils.okhttp3.OkHttpUtil;

/**
 * 股东人数线程
 */
public class StockHolderInfoThread implements Runnable {
    private static final String API = "https://datacenter.eastmoney.com/securities/api/data/v1/get?reportName=RPT_F10_EH_HOLDERNUM&columns=SECUCODE%2CSECURITY_CODE%2CEND_DATE%2CHOLDER_TOTAL_NUM%2CTOTAL_NUM_RATIO%2CAVG_FREE_SHARES%2CAVG_FREESHARES_RATIO%2CHOLD_FOCUS%2CPRICE%2CAVG_HOLD_AMT%2CHOLD_RATIO_TOTAL%2CFREEHOLD_RATIO_TOTAL&quoteColumns=&filter=(SECUCODE%3D%22STOCK_CODE_WITH_MARKET%22)&pageNumber=PAGE_NUMBER_WITH_MARKET&pageSize=PAGE_SIZE_WITH_MARKET&sortTypes=-1&sortColumns=END_DATE&source=HSF10&client=PC&v=032046264153815407";
    private final String code;
    private final StockHolderNumsInfoService stockHolderNumsInfoService;

    public StockHolderInfoThread(String code, StockHolderNumsInfoService stockHolderNumsInfoService) {
        this.code = code;
        this.stockHolderNumsInfoService = stockHolderNumsInfoService;
    }

    @Override
    public void run() {
        try {
            DfcfResponse<StockHolderNumsInfo> response = getResponse(1, 10);
            if (response != null && response.isSuccess()) {
                stockHolderNumsInfoService.insert(response.getResult().getData());
                int totalPages = response.getResult().getPages();
                if (totalPages > 1) {
                    for (int i = 2; i <= totalPages; i++) {
                        DfcfResponse<StockHolderNumsInfo> pageResponse = getResponse(i, 10);
                        if (pageResponse != null && pageResponse.isSuccess()) {
                            stockHolderNumsInfoService.insert(pageResponse.getResult().getData());
                        } else {
                            System.err.println("请求失败: " + pageResponse.getMessage());
                        }
                    }
                }
            } else {
                System.err.println("请求失败: " + response.getMessage());
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private DfcfResponse<StockHolderNumsInfo> getResponse(int pageNumber, int pageSize) {
        String stock_code = "";
        if (code.startsWith("6")) {
            stock_code = code + ".SH";
        } else {
            stock_code = code + ".SZ";
        }
        String result = OkHttpUtil.getRequest(API.replace("STOCK_CODE_WITH_MARKET", stock_code)
                .replace("PAGE_NUMBER_WITH_MARKET", String.valueOf(pageNumber))
                .replace("PAGE_SIZE_WITH_MARKET", String.valueOf(pageSize)), null);
        return JSON.parseObject(
                result,
                new TypeReference<DfcfResponse<StockHolderNumsInfo>>() {
                }
        );
    }
}
